<?php

#--------------------------------------------------
# ALL INCLUDES + STANDARD-VALUES
#--------------------------------------------------
include_once ("include/config.inc");
include_once ("include/template.inc");
include_once ("include/class.db.php5");
include_once ("include/class.tools.php5");
$svo = new db("svoptik");
$tools = new tool();

include_once ("include/global_fix_extern.inc");

#--------------------------------------------------
# CREATE TEMPLATE
#--------------------------------------------------
$t = new Template("templates", "keep");
$t->set_file(array (
	"page" => "svoptik_popup.tpl",
	"comment" => "photo_comment.tpl"
));
#--------------------------------------------------
$t->set_var(array (
	"FORWARD" => ""
));

#--------------------------------------------------
# CREATE CONTENT "NEW PHOTO-COMMENT"
#--------------------------------------------------
global $photo_id, $com_name, $com_text, $com_email, $send;

function get_photo($id) {
	global $svo, $table, $tools;

	$photo = $svo->readRecordset("select p.*, g.* from " . $table['Photos'] . " as p, " . $table['Galerien'] . " as g where photo_id=" . $id . " && g.gal_id=p.gal_id;");
	$photo_data['IMG_SRC'] = "
	        <img src=\"images/gallery/" . $photo['gal_dir'] . "/" . $photo['photo_thb'] . "\" border=\"0\" alt=\"\" title=\"" . $photo['photo_txt'] . "\" align=\"left\" hspace=\"15\">" . $photo['photo_txt'] . "<br>Views: " . $photo['photo_views'] . "<br>
	      ";
	$photo_data['GAL'] = "<b>" . $tools->html_replace($photo['gal_bez']) . "</b><br><font class=\"hint\">" . $tools->show_date($photo['gal_date'], "date_only") . "</font>";
	return $photo_data;
}

function get_comments($id) {
	global $svo, $table, $tools;

	$get_com = $svo->doSQL("select * from " . $table['Kommentare'] . " where photo_id=" . $id . " order by com_date desc limit 5;");
	if ($svo->countRows($get_com) > 0) {
		$comments = "
		          <table width=\"100%\"cellspacing=\"5\" cellpadding=\"0\" border=\"0\">
		          ";
		while ($row = $svo->getRecordset($get_com)) {
			$comments .= "
			            <tr>
			                <td align=\"left\" valign=\"top\" width=\"100\" class=\"hint\"><br>" . $tools->html_replace($row['com_name']) . "</td>
			                <td align=\"left\" valign=\"top\" class=\"hint\"><br>" . $tools->html_replace($row['com_text']) . "</td>
			            </tr>
			              ";
		}
		$comments .= "
		          </table>
		          ";
	} else
		$comments = "&nbsp;";
	return $comments;
}

$photo_data = get_photo($photo_id);
$t->set_var(array (
	"NAME" => $com_name,
	"TEXT" => $com_text,
	"EMAIL" => $com_email,
	"LAST" => get_comments($photo_id
), "PHOTO_ID" => $photo_id, "GAL" => $photo_data['GAL'], "PHOTO" => $photo_data['IMG_SRC'], "MSG" => ""));

if (!$send)
	$content = $t->parse("", "comment", true);
else {
	$content = "
	      <script language=\"javascript\">
	      <!--
	      function win_close() {
	          opener.location.replace('photo.php5?photo_id=" . $photo_id . "');
	          window.close();
	      }
	      //-->
	      </script>
	      ";
	if (empty ($com_name) || empty ($com_email) || empty ($com_text) || !ereg("^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})", $com_email)) {
		$t->set_var(array (
			"MSG" => "Bitte &uuml;berpr&uuml;fen Sie Ihre Eingaben!"
		));
		$content .= $t->parse("", "commment", true);
	} else {
		if ($svo->doSQL("insert into " . $table['Kommentare'] . " (photo_id, com_name, com_email, com_text, com_date) values('" . $photo_id . "', '" . $com_name . "', '" . $com_email . "', '" . $com_text . "', '" . date("Y-m-d") . "');"))
			$content .= "<p align=\"center\" class=\"text\">Kommmentar wurde gespeichert!</p><script language=\"Javascript\"> window.setTimeout(win_close(), 5000) </script>";
		else
			$content .= "
			          <p align=\"center\" class=\"text\">Kommmentar wurde NICHT gespeichert!</p>
			          <p align=\"center\" class=\"text\"><a href=\"javascript:history.back();\" class=\"text\" title=\"zur&uuml;ck\">zur&uuml;ck</a></p>
			          ";
	}
}

#--------------------------------------------------

#--------------------------------------------------
# PARSE & OUTPUT
#--------------------------------------------------
$t->set_var(array (
	"NAVI" => "<img src=\"standard/spacer.gif\" width=\"10\" height=\"20\" border=\"0\" alt=\"\">",
	"sitenav" => "BILDERGALERIEN -> PHOTO -> KOMMENTIEREN",
	"content" => $content
));

$t->parse("ALL", "page", true);
$t->p("ALL");
?>
